﻿
Imports System.Net.Sockets

Namespace socketTrans
    Public Module socketTrans
        Private ReadOnly keepalive As bytes = Nothing

        Sub New()
            keepalive += convertor.tobytes(1.as(Of Int32)())
            keepalive += convertor.tobytes((30 * 1000).as(Of Int32)())
            keepalive += convertor.tobytes((5 * 1000).as(Of Int32)())
        End Sub

        Public Sub initial(ByVal socket As Socket)
            assert(Not socket Is Nothing)
            socket.NoDelay() = True
            socket.SendBufferSize() = 0
            socket.IOControl(IOControlCode.KeepAliveValues, +keepalive, Nothing)
        End Sub

        Public Sub close(ByRef socket As Socket)
            If Not socket Is Nothing AndAlso socket.Connected() Then
                socket.Shutdown(SocketShutdown.Both)
                socket.Close()
                socket = Nothing
            End If
        End Sub
    End Module
End Namespace
